﻿using AsposeHelper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QD.DbManager
{
    public partial class SaveFileMag : DbBasic
    {
        private string exportType = "excel";
        /// <summary>
        /// 导出类型，现只支持 excel 与 word
        /// </summary>
        public string ExportType
        {
            get { return exportType; }
            set { exportType = value; }
        }

        private DataTable dt_export;
        private TreeView tn_export;

        /// <summary>
        /// 文档标题
        /// </summary>
        private string title;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dt_export">dt_export需导出的数据集</param>
        public SaveFileMag(DataTable dt_export)
        {
            InitializeComponent();
            this.dt_export = dt_export;

            this.StartPosition = FormStartPosition.CenterScreen;
            this.FormBorderStyle = FormBorderStyle.FixedSingle;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="title">文档标题</param>
        /// <param name="dt_export">kv_export需导出的数据集</param>
        public SaveFileMag(string title, TreeView tn_export)
        {
            InitializeComponent();
            this.tn_export = tn_export;
            this.title = title;

            this.StartPosition = FormStartPosition.CenterScreen;
            this.FormBorderStyle = FormBorderStyle.FixedSingle;
        }

        private void SaveFileMag_Load(object sender, EventArgs e)
        {
        }

        private void button24_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                this.txtSavePath.Text = dialog.SelectedPath;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string savePath = this.txtSavePath.Text.Trim();
            if (string.IsNullOrEmpty(savePath))
            {
                MessageBox.Show("请选择保存位置！");
            }
            else
            {
                switch (ExportType)
                {
                    case "excel":
                        {
                            Dictionary<string, DataTable> data = new Dictionary<string, DataTable>();
                            data.Add(dt_export.TableName, dt_export);
                            CellsHelper.ExportExcel(data, savePath, 1, dt_export.TableName);
                            break;
                        }
                    case "word":
                        {
                            WordsHelper helper = new WordsHelper();
                            List<WordSection> content = new List<WordSection>();
                            for (int i = 0; i < tn_export.Nodes.Count; i++)
                            {
                                string serial = (i + 1).ToString();
                                Re_InitContent(tn_export.Nodes[i], serial, content);
                            }

                            helper.ExportWords(savePath, title, content);
                            break;
                        }
                }

                MessageBox.Show("导出成功！");
                this.Close();
            }
        }

        //初始化要写入word文档的内容
        private void Re_InitContent(TreeNode node, string serial, List<WordSection> content)
        {
            string level = (node.Level + 1).ToString();
            string index = (node.Index + 1).ToString();
            string prefix = serial + "." + index;
            string title = "<h" + level + ">" + prefix + " " + node.Text + "</h" + level + ">";
            string value = (node.Tag == null ? "" : node.Tag.ToString().Replace("\n\n", "\n"));

            WordSection _content = new WordSection();
            _content.title = title;
            _content.Content = value;
            _content.Level = node.Level;

            content.Add(_content);
            if (node.Nodes.Count > 0)
            {
                foreach (TreeNode _node in node.Nodes)
                {
                    Re_InitContent(_node, prefix, content);
                }
            }
        }
    }
}
